<?php
namespace Api\Controller;
use Think\Controller;

class CartController extends BaseController {

   private $m = NULL;
   private $md = NULL;
   private $navName = '订单管理';


   public function _initialize(){
   		parent::_initialize();
		$this->m = M('shop_cart');
    }
    
    
    
    public   function  add(){
       $mid = 3;
       $cid = I('cid');
       $pro_id = I('pro_id');
       
       $is_id = $this->m->where('cid="'.$cid.'" AND pro_id="'.$pro_id.'"')->getField('id');
       if( $is_id ){
           $result = $this->m->where('id="'.$is_id.'"')->setInc('pro_count',1);
        }else{
            $info['cid'] = $cid;
            $info['mid'] = $mid;
            $info['pro_id'] = $pro_id;
            $info['pro_count'] = 1;
            $info['add_time'] = time();
            $result01 = $this->m->add($info);
            
        } 
       
         
         
       if(  $result  ||  $result01 ){
           
          // returnApiresult("0", "添加成功");exit;
           returnApiData2('0','添加成功',$data, $data2,$data3,$data4);exit;	 
       }
       
   
       
    }
    
    
    
    public   function  del(){
       //$mid = I('mid');
        $mid = 3;
        $cid = 22;
        $pro_id = I('pro_id');
       
        $is_info = $this->m->where('cid="'.$cid.'" AND pro_id="'.$pro_id.'"')->find();
        
        if( $is_info ){
           if(   $is_info['pro_count'] >= 1  ){
              $result = $this->m->where('id="'.$is_info['id'].'"')->setDec('pro_count',1);
            }
        }
          
        
       if(  $result ){
           
          //  returnApiresult("0", "减1成功");exit;
             returnApiData2('0','减1成功',$data, $data2,$data3);exit;	
        }else{
           //  returnApiresult("0", "该产品数量已经为0");exit;
             returnApiData2('0','该产品数量已经为0',$data, $data2,$data3);exit;	
            
        }

       }
    	 	
	
       
       public function  account(){
           $cid = I('cid');
           $mid = 3;
        
          $info = M('customer_shipping')->where('cid="'.$cid.'"')->order('add_time desc')->find(); 
      
          if( $info ){
              $data['linkman'] = $info['linkman'];
              $data['sex'] = $info['sex']=1?"女士":"男士";
              $data['link_phone'] = $info['link_phone'];
              $data['ship_address'] = $info['ship_address'];
              $data['ship_prv'] = M('shop_address')->where('id="'.$info['ship_prv'].'"')->getField('name');
              $data['ship_city'] = M('shop_address')->where('id="'.$info['ship_city'].'"')->getField('name');
           }else{
              $data['tishi'] = '现在请添加';
           }
         
//         
//          $ship_address = M('customer_shipping')->where('cid="'.$cid.'"')->order('add_time desc')->select();
//        
//          foreach ( $ship_address as $key => $item ){
//            $data1[$key]['id'] = $item['id'];
//            $data1[$key]['linkman'] = $item['linkman'];
//            $data1[$key]['link_phone'] = $item['link_phone'];
//            $data1[$key]['ship_address'] = $item['ship_address'];
//          }
        
         
          $shop_info= M('shop')->where('mid="'.$mid.'"')->find();
          $data2['shop_name'] = $shop_info['shop_name'];
          $data2['shop_heapic'] = M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic'):""; //店铺头像
          $last_time_pro_cart = M('shop_cart')->where('cid="'.$cid.'" AND mid="'.$mid.'" ')->order('add_time  desc')->limit(0,1)->select();
          $the_time  = $last_time_pro_cart['add_time'];
          $return_time = $this->time_tran($the_time);
          $data2['click_time'] =   $return_time;
          
     
          $pros = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->select();
          $sum =  0;
          foreach ( $pros  as  $key => $item){
               $data3[$key]['pro_name'] = M('shop_product')->where('id="'.$item['pro_id'].'"')->getField('pro_name');
               $data3[$key]['pro_count'] = $item['pro_count'];
               $data3[$key]['price'] = M('shop_product')->where('id="'.$item['pro_id'].'"')->getField('price');
               $sum += $data3[$key]['pro_count'] * $data3[$key]['price'];
          }
         
           
         $data4['pro_money_total'] = $sum;
         
     
                   $cart_ids = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->field('id')->select();
                   //dump($cart_ids);exit;
                   $sum1=0;//设变量
                   foreach ( $cart_ids  as $key => $item ){
                        //echo $item['id'];
                        $pro_id = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_id');
                        $price = M('shop_product')->where('id="'.$pro_id.'"')->getField('price');
                        $pro_count = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_count');
                        $pro_money = $pro_count * $price;
                        $sum1 += $pro_money;
                        
                    }
                   
                   $sid =  M('shop')->where('mid="'.$mid.'"')->getField('id');
                   $delivery_fee =  M('shop_detail')->where('id="'.$sid.'"')->getField('delivery_fee');
                   $pro_num_all = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->sum('pro_count');
                   $data1['pro_num_all'] = $pro_num_all;
                   $data1['delivery_fee'] = $delivery_fee;
                   $data1['pro_money_all'] = $sum1;
                  
      
                  returnApiData6('0','成功',$data,$data2,$data3,$data4,$data1);exit;	
           
       }
       
       
       
        
   function time_tran($the_time) {  
    $now_time = date("Y-m-d H:i:s", time());  
  
    $now_time = strtotime($now_time); 
    
    $show_time = $the_time;  
    $dur = $now_time - $show_time;  
     
    if ($dur < 0) {  
        return $the_time;  
    } else {  
        if ($dur < 60) {  
            return $dur . '秒前';  
        } else {  
            if ($dur < 3600) {  
                return floor($dur / 60) . '分钟前';  
            } else {  
                if ($dur < 86400) {  
                    return floor($dur / 3600) . '小时前';  
                } else {  
                    if ($dur < 259200) {//3天内  
                        return floor($dur / 86400) . '天前';  
                    } else {  
                       // return $the_time;
                        return '超过3天前';
                    }  
                }  
            }  
        }  
    }  
}  
   
       
       

    public  function   account2(){
        $cid = 22;
        $mid = 3;
        $ship_id = I('ship_id');
        if( $ship_id ){      
             $sh = M('customer_shipping')->where('cid="'.$cid.'" AND id="'.$ship_id.'"')->setField('add_time', time());
        }
       
        
        $cart_ids = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->field('id')->select();
        $order_no = $this->GetorderStr();    
        $sum=0;//设变量
        $shop_id = M('shop')->where('mid="'.$mid.'"')->getField('id');
        $delivery_fee = M('shop_detail')->where('s_id="'.$shop_id.'"')->getField('delivery_fee');
        foreach ( $cart_ids  as $key => $item ){
                       
                 $info['from_mid'] = $mid;
                 $info['cid'] = $cid;
                 $info['pro_id'] = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_id');
                 $info['cost_price'] = M('shop_product')->where('id="'.$info['pro_id'].'"')->getField('price');
                 $info['delivery_fee'] = $delivery_fee;
                 $info['buy_count'] = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_count');
                 $info['total_cost'] = $info['cost_price'] * $info['buy_count'];  //商品单价* 数量
                 $info['order_no'] = $order_no;
                 $info['add_time'] = time();
                 $info['cost_type'] =  I('cost_type'); //消费类型  1，店内消费   2，校边外卖  3，影院商家
                 $sum += $info['total_cost'];
                 $result = M('shop_product_order')->add($info);
                 
        }
         
              if( $result){    // 循环删除
                   M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->delete();
                }
               $data1['order_total'] = $sum + $delivery_fee; //商品单价* 数量   +   配送费
          
               
               $info2['note'] = I('note');
               $info2['order_no'] = $order_no;
               $info2['add_time'] = time;
               $info2['cid'] = $cid;
               $info2['mid'] = $mid;
               $info2['order_total'] =  $data1['order_total'];
               M('shop_product_order_pro')->add($info2);
               
               returnApiData2('0','订单生成成功',$data1, $data2,$data3);exit;	    
    }
    
    
    //随机订单号
function GetorderStr(){

        $newoutputstr=date('Ymdhis').rand(1000,9999);
  
        $num = M("shop_product_order")->where('order_no="'.$newoutputstr.'"')->count();
	
  	   if($num ==0){
	    return $newoutputstr;
	   }else{
            return GetorderStr();	
	   }
  
   }
   
   
   
   
   public  function  mov_account(){
       $cid = I('cid');
       $mid = I('mid');
       $mov_money= I('mov_money');
       
          $info = M('customer_shipping')->where('cid="'.$cid.'"')->order('add_time desc')->find(); 
      
          if( $info ){
              $data['linkman'] = $info['linkman'];
              $data['sex'] = $info['sex']=1?"女士":"男士";
              $data['link_phone'] = $info['link_phone'];
           }else{
              $data['tishi'] = '现在请添加';
           }
         
           
          $shop_info= M('shop')->where('mid="'.$mid.'"')->find();
          $data2['shop_name'] = $shop_info['shop_name'];
          $data2['shop_heapic'] = M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic'):""; //店铺头像
          $last_time_pro_cart = M('shop_cart')->where('cid="'.$cid.'" AND mid="'.$mid.'" ')->order('add_time  desc')->limit(0,1)->select();
          $the_time  = $last_time_pro_cart['add_time'];
          $return_time = $this->time_tran($the_time);
          $data2['click_time'] =   $return_time;
          
          
          $data3['pro_name'] =  '电影票';
          if( $mov_money  && $mov_money > 0 ){
                $data3['pro_money_total'] = $mov_money;
          }
        
                  
          returnApiData6('0','成功',$data,$data2,$data3,$data4,$data1);exit;	
        }   
   
   
   
   
   
   

  }